﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Active.Core;
namespace Active.Controls
{
    public class MediaBill : Control
    {
        #region Properties
        private string _CssClass = "";
        /// <summary>
        /// 样式
        /// </summary>
        public string CssClass
        {
            get { return _CssClass; }
            set { _CssClass = value; }
        }
        private Order _CurrentOrder = null;
        /// <summary>
        /// 当前Order
        /// </summary>
        public Order CurrentOrder
        {

           get
            {
                if (_CurrentOrder == null)
                {
                    _CurrentOrder = GetOrder();
                }
                return _CurrentOrder;
            }
            //set { _CurrentOrder = value; }
        }
        private MediaOrders _CurrentMediaOrders=null;
        /// <summary>
        /// Order下的媒体信息
        /// </summary>
        private MediaOrders CurrentMediaOrders
        {
            get
            {
                if (CurrentOrder == null)
                    return null;
                return CurrentOrder.MediaOrders;
            }
        }
        private decimal _TotalRealIncome = 0;
        /// <summary>
        /// 总收入
        /// </summary>
        /// <returns></returns>
        private decimal TotalRealIncome
        {
            get { return _TotalRealIncome; }
            set { _TotalRealIncome = value; }
        }
        private int _TotalTimes = 0;
        /// <summary>
        /// 总次数
        /// </summary>
        private int TotalTimes
        {
            get { return _TotalTimes; }
            set { _TotalTimes = value; }
        }
        private int _MainRows = CompanySettings.Instance.MediaBillMainRows;
        /// <summary>
        /// 媒体订单行数,包括备注行
        /// </summary>
        public int MainRows
        {
            get { return _MainRows; }
            set { _MainRows = value; }
        }
        private int _DescriptionRows = -1;
        /// <summary>
        /// 备注行数
        /// </summary>
        private int DescriptionRows
        {
            get 
            {
                if (_DescriptionRows < 0)
                    return _DescriptionRows = CurrentOrder.MediaDescription.Split(new string[] { "\r\n" },StringSplitOptions.RemoveEmptyEntries).Length;
                return _DescriptionRows; 
            }
            
        }
        private int _PageCount = 0;
        /// <summary>
        /// 总页数
        /// </summary>
        private int PageCount
        {
            get 
            {
                if (_PageCount != 0)
                    return _PageCount;
                if (DescriptionRows >= MainRows)
                    return _PageCount = -1;
                if (CurrentMediaOrders.Count % (MainRows - DescriptionRows) == 0)
                    return _PageCount = CurrentMediaOrders.Count / (MainRows - DescriptionRows);
                else
                    return _PageCount = CurrentMediaOrders.Count / (MainRows - DescriptionRows) + 1;
            }
        }
        /// <summary>
        /// 每页MediaOrder行数
        /// </summary>
        private int PageSize 
        {
            get { return MainRows - DescriptionRows; }
        }
        private bool _IsPager = false;
        /// <summary>
        /// 是否分页
        /// </summary>
        public bool IsPager
        {
            get { return _IsPager; }
            set { _IsPager = value; }
        }
        #endregion

        #region mathod
        private Order GetOrder()
        {
            string idString;
            if (IsPager)
                idString = Page.Request.QueryString["orderId"];
            else
                idString = Page.Request.QueryString["id"];
            object businessObj = Page.Session["CurrentObj"];
            if (businessObj == null || !(businessObj is Order)
                || (!Page.IsPostBack && !((Order)businessObj).Id.ToString().Equals(idString)))
            {
                try
                {
                    if (!string.IsNullOrEmpty(idString))
                    {
                        businessObj = Order.GetOrder(int.Parse(idString));
                    }
                    else
                    {
                        //businessObj = Order.NewOrder();
                        businessObj = null;
                    }
                    Page.Session["CurrentObj"] = businessObj;
                }
                catch (System.Security.SecurityException)
                {
                    Page.Response.Redirect("~/Bill");
                }
            }
            return businessObj as Order;
        }
        /// <summary>
        /// writeMonth
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="writer"></param>
        private void WriteMonth(DateTime startDate,HtmlTextWriter writer)
        {
            int allDates=DateTime.DaysInMonth(startDate.Year, startDate.Month);
            int datesInMonth=allDates - startDate.Day + 1;
            if (datesInMonth < 32)
            {
                writer.AddAttribute("colspan", datesInMonth.ToString());
                writer.AddAttribute("class", "left");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(startDate.Month.ToString() + "月");
                writer.RenderEndTag();

                writer.AddAttribute("colspan", ((int)(32 - datesInMonth)).ToString());
                writer.AddAttribute("class", "left");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(((int)(startDate.Month + 1)).ToString() + "月");
                writer.RenderEndTag();
            }
        }
        /// <summary>
        /// writeDate
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="writer"></param>
        private void WriteDate(DateTime startDate, HtmlTextWriter writer)
        {
            int allDates = DateTime.DaysInMonth(startDate.Year, startDate.Month);
            int datesInMonth = allDates - startDate.Day + 1;
            if (datesInMonth < 32)
            {
                int day=startDate.Day;
                for (int i = 0; i < datesInMonth; i++)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(day.ToString());
                    writer.RenderEndTag();
                    day++;
                }

                for (int i = 1; i <=(32 - datesInMonth); i++)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(i.ToString());
                    writer.RenderEndTag();
                }
            }
            else
            {
                for (int i = 1; i <= 31; i++)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(i.ToString());
                    writer.RenderEndTag();
                }
            }
        }
        /// <summary>
        /// WriteTableBody
        /// </summary>
        /// <param name="write"></param>
        private void WriteTableBody(int start,int end,HtmlTextWriter writer)
        {
            TotalTimes = 0;
            TotalRealIncome = 0;
            for (int index = start; index <= end;index++ )
            {
                TotalRealIncome += CurrentMediaOrders[index].Payout;
                if (CurrentMediaOrders[index].Category != OrderType.Additional)
                    TotalTimes += CurrentMediaOrders[index].Times;
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                //
                if (CurrentOrder.Media.Category == MediaType.Publication)
                    writer.AddAttribute("colspan", "2");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                string strNameAndChannel = (CurrentOrder.Media.Name == CurrentMediaOrders[index].Channel) ? CurrentMediaOrders[index].Channel : CurrentOrder.Media.Name + "<br/>" + CurrentMediaOrders[index].Channel;
                writer.Write(strNameAndChannel);
                writer.RenderEndTag();
                //
                if (CurrentOrder.Media.Category == MediaType.TV)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(CurrentMediaOrders[index].TimeSlot);
                    writer.RenderEndTag();
                }
                //
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(CurrentMediaOrders[index].Position);
                writer.RenderEndTag();
                //单价
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                if (CurrentMediaOrders[index].Category == OrderType.Pay)
                {
                    //if(CurrentMediaOrders[index].UnitPrice>0)
                        writer.Write(Formater.FormatPrice(CurrentMediaOrders[index].UnitPrice,"0.00"));
                    //else
                    //writer.Write(" ");
                }
                else if (CurrentMediaOrders[index].Category == OrderType.Series)
                {
                    writer.Write("套播");
                }
                else if (CurrentMediaOrders[index].Category == OrderType.Present)
                {
                    writer.Write("赠送");
                }
                else if (CurrentMediaOrders[index].Category == OrderType.Additional)
                {
                    writer.Write("追加款");
                }
                writer.RenderEndTag();
                //规格
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(CurrentMediaOrders[index].Duration);
                writer.RenderEndTag();
                //
                string[] campaignPeriod = CurrentMediaOrders[index].CampaignPeriod.Split('|');
                for (int i = 0; i < campaignPeriod.Length; i++)
                {
                    if (campaignPeriod[i].Trim()=="0")
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write("&nbsp; &nbsp;");
                        writer.RenderEndTag();
                    }
                    else
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write(campaignPeriod[i]);
                        writer.RenderEndTag();
                    }
                }
                for (int i = 1; i <= (32 - campaignPeriod.Length); i++)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("&nbsp; &nbsp;");
                    writer.RenderEndTag();
                }
                //
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(CurrentMediaOrders[index].Times.ToString());
                writer.RenderEndTag();
                //
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                double discount = CurrentMediaOrders[index].Discount * 100;
                if(discount>0)
                writer.Write(string.Format("{0}%", discount));
                else
                writer.Write("-");
                writer.RenderEndTag();
                //实付
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                //if (CurrentMediaOrders[index].Category == OrderType.Pay || CurrentMediaOrders[index].Category == OrderType.Additional)
                //    writer.Write(CurrentMediaOrders[index].Payout > 0 ? Formater.FormatPrice(CurrentMediaOrders[index].Payout) : " ");
                //else if (CurrentMediaOrders[index].Category == OrderType.Series)
                //{
                //    writer.Write("套播");
                //}
                //else if (CurrentMediaOrders[index].Category == OrderType.Present)
                //{
                //    writer.Write("赠送");
                //}
                //else if (CurrentMediaOrders[index].Category == OrderType.Additional)
                //{
                //    writer.Write("追加款");
                //}
                writer.Write(Formater.FormatPrice(CurrentMediaOrders[index].Payout, CurrentMediaOrders[index].Category));
                writer.RenderEndTag();
                //EndRow
                writer.RenderEndTag();
            }
        }
        /// <summary>
        /// WriteTable
        /// </summary>
        /// <param name="mediaOrders"></param>
        /// <param name="writer"></param>
        private void WriteTable(int start,int end,int pageNo, HtmlTextWriter writer)
        {
            #region printDiv
            writer.AddAttribute("id", "printDiv" + pageNo.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            #region Table
            if (CssClass != "")
                writer.AddAttribute("class", CssClass);
            else
                writer.AddAttribute("class", "normal");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            #region 第一行
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //
            writer.AddAttribute("colspan", "13");
            writer.AddAttribute("class", "left fontLarger noneborder");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("<img src='" + Utils.RelativeWebRoot + "theme/stander/img/ActiveLogo_white.jpg' />" + CompanySettings.Instance.Name);
            writer.RenderEndTag();
            //
            writer.AddAttribute("colspan", "27");
            writer.AddAttribute("rowspan", "2");
            writer.AddAttribute("class", "right noneborder");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("请确认回传!<br/>(" + pageNo.ToString() + "/" + PageCount.ToString() + ")<br/>"
                + CompanySettings.Instance.Address + "<br/>TEL:" + Formater.FormatTelFax(CompanySettings.Instance.Tel) + "&nbsp; &nbsp;FAX:" + Formater.FormatTelFax(CompanySettings.Instance.Fax));
            writer.RenderEndTag();
            //EndRow
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //
            writer.AddAttribute("colspan", "13");
            writer.AddAttribute("class", "left noneborder");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            string strMediaTypt = CurrentOrder.Media.Category==MediaType.TV ? "电视广告订单" : "报纸广告订单";
            writer.Write(strMediaTypt);
            writer.RenderEndTag();
            writer.RenderEndTag();
            #endregion

            #region 第二行
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute("colspan", "2");
            writer.AddAttribute("class", "left");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("客户Client:" + "<br/>"+CurrentOrder.Bill.ProductName); 
            writer.RenderEndTag();
            //
            writer.AddAttribute("colspan", "3");
            writer.AddAttribute("class", "left");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            string strVesion = CurrentOrder.Media.Category == MediaType.TV ? "播出版本:<br/>"+Formater.PlayVersions(CurrentOrder.PlayVersions) : "产品Product:<br/>"+CurrentOrder.Bill.ProductName;
            writer.Write(strVesion);
            writer.RenderEndTag();
            //
            writer.AddAttribute("colspan", "32");
            writer.AddAttribute("class", "left");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("广告周期Campaign Period:"+"<br/>"+CurrentOrder.StartDate.Date.ToShortDateString()+"~"+CurrentOrder.EndDate.Date.ToShortDateString());
            writer.RenderEndTag();
            //
            writer.AddAttribute("colspan", "3");
            writer.AddAttribute("class", "left");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("确认日期:"+CurrentOrder.MediaPreparedDate.ToString("yyyy-M-d"));
            writer.RenderEndTag();
            //EndRow
            writer.RenderEndTag();
            #endregion

            #region 第三行
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //
            if(CurrentOrder.Media.Category == MediaType.Publication)
                writer.AddAttribute("colspan", "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-bottom-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("媒体");
            writer.RenderEndTag();
            //
            if (CurrentOrder.Media.Category == MediaType.TV)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-bottom-width: 0px;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("时段");
                writer.RenderEndTag();
            }
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-bottom-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("位置");
            writer.RenderEndTag();
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-bottom-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("单价");
            writer.RenderEndTag();
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-bottom-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("规格");
            writer.RenderEndTag();
            //
            //WriteMonth(CurrentOrder.Bill.StartDate, writer);
            WriteMonth(CurrentOrder.StartDate.Date, writer);
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-bottom-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("次数");
            writer.RenderEndTag();
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-bottom-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("优惠率");
            writer.RenderEndTag();
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-bottom-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("实付");
            writer.RenderEndTag();
            //EndRow
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //
            if (CurrentOrder.Media.Category == MediaType.Publication)
                writer.AddAttribute("colspan", "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-top-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Station");
            writer.RenderEndTag();
            //
            if (CurrentOrder.Media.Category == MediaType.TV)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-top-width: 0px;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("Time slot");
                writer.RenderEndTag();
            }
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-top-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Position");
            writer.RenderEndTag();
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-top-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Net rate");
            writer.RenderEndTag();
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-top-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Duration");
            writer.RenderEndTag();
            //
            WriteDate(CurrentOrder.StartDate.Date, writer);
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-top-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Fre.");
            writer.RenderEndTag();
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-top-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Discount");
            writer.RenderEndTag();
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-top-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Total Rate");
            writer.RenderEndTag();
            //EndRow
            writer.RenderEndTag();
            #endregion

            #region TableBody
            WriteTableBody(start,end,writer);
            #endregion

            #region 合计行...
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //
            writer.AddAttribute("colspan", "31");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-right-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.RenderEndTag();
            //
            writer.AddAttribute("colspan", "6");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-left-width: 0px;border-right-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("合计:");
            writer.RenderEndTag();
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-left-width: 0px;border-right-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(TotalTimes.ToString());
            writer.RenderEndTag();
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-left-width: 0px;border-right-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.RenderEndTag();
            //
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-left-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(Formater.FormatPrice(TotalRealIncome,"0.00"));
            writer.RenderEndTag();
           
            //EndTr
            writer.RenderEndTag();
            #endregion

            #region 备注行
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //
            writer.AddAttribute("colspan", "40");
            writer.AddAttribute("class", "left");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            writer.Write("备注:" + "<br/>" + CurrentOrder.MediaDescription.Replace("\r\n","<br/>"));
            writer.RenderEndTag();
            //EndTr
            writer.RenderEndTag();
            #endregion

            #region 签名行
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //
            writer.AddAttribute("colspan", "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-right-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.RenderEndTag();
            //
            writer.AddAttribute("colspan", "20");
            writer.AddAttribute("class", "left");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-left-width: 0px;border-right-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(CompanySettings.Instance.Name + "(盖章):<br/>"+"签名:<br/>"+"审核:<br/>"+"日期:");
            writer.RenderEndTag();
            //
            writer.AddAttribute("colspan", "18");
            writer.AddAttribute("class", "left");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-left-width: 0px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("媒体确认(盖章):<br/>" + "签名:<br/><br/>" + "日期:");
            writer.RenderEndTag();
            //EndTr
            writer.RenderEndTag();
            #endregion

            //EndTable
            writer.RenderEndTag();
            #endregion
            //EndDiv
            writer.RenderEndTag();
            //打印按钮
            writer.AddAttribute("type", "button");
            writer.AddAttribute("class", "button");
            if (IsPager)
            {
                writer.AddAttribute("value", "打印第" + pageNo.ToString() + "页");
                writer.AddAttribute("onclick", string.Format("javascript:window.open('{0}')", Utils.RelativeWebRoot+"Print.aspx?id=" + "printDiv" + pageNo.ToString()));
            }
            else
            {
                writer.AddAttribute("value", "打   印");
                if (PageCount == 1)
                    writer.AddAttribute("onclick", string.Format("javascript:window.open('{0}')", Utils.RelativeWebRoot + "Print.aspx?id=" + "printDiv" + pageNo.ToString()));
                else if (PageCount > 1)
                {
                    writer.AddAttribute("onclick", string.Format("javascript:window.open('{0}')", Utils.RelativeWebRoot + "PrintPager.aspx?orderId=" + CurrentOrder.Id.ToString()));
                }
            }
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
            #endregion
        }
        #endregion

        #region override
        protected override void Render(HtmlTextWriter writer)
        {
            if (CurrentMediaOrders == null || CurrentMediaOrders.Count==0)
            {
                writer.AddAttribute("style", "color:Red; font-size:x-large;");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                if(!IsPager)
                writer.Write("该订单无任何媒体订单");
                writer.RenderEndTag();
                return;
            }
            if (PageCount == -1)
            {
                writer.AddAttribute("style", "color:Red; font-size:x-large;");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.Write("该订单的媒体备注太多,容不下任何媒体订单.");
                writer.RenderEndTag();
                return;
            }
            if (IsPager)
            {
                for (int pageNo = 1; pageNo <= PageCount; pageNo++)
                {
                    if (pageNo == PageCount)
                    {
                        int tempPageSize = CurrentMediaOrders.Count - (pageNo - 1) * PageSize;
                        WriteTable((pageNo - 1) * PageSize, CurrentMediaOrders.Count - 1, pageNo, writer);
                    }
                    else
                    {
                        WriteTable((pageNo - 1) * PageSize, pageNo * PageSize-1, pageNo, writer);
                    }
                }

            }
            else
            {
                WriteTable(0,CurrentMediaOrders.Count-1, 1, writer);
            }

        }
        #endregion
    }
}
